<?php 
//管理员权限类控制器
namespace app\admin\controller;
use app\common\controller\AdminService as AdminService;
use think\Controller;
use think\Request;
class Admin extends BaseController{
	public function index(){
		//管理员用户列表
		$service=new AdminService;
		$adminUserList=$service->selectAdminUser();
		$this->assign('adminUserList',$adminUserList);
		return $this->fetch('system/admin');
	}
	public function add_admin(Request $request){
		if($request->isPost()){
			$user['username']=$request->param('username');
			$user['password']=md5($request->param('password'));
			$user['level']=$request->param('level');
			$service=new AdminService;
			if($res=$service->addAdminUser($user)){
				$this->success('用户[ ' . $res->username . ':' . $res->id . ' ]新增成功');
	        } else {
	            return $res->getError();
	        }
		}
		return $this->fetch('system/adminAdd');
	}
	//管理员编辑
	public function edit_admin(Request $request){
		$id=$request->param('id');
		$service=new AdminService;
		$adminUserItem=$service->selectAdminUser($id);
		$this->assign('userInfoItem',$adminUserItem);
		return $this->fetch('system/adminEdit');
	}
	//管理员修改
	public function update_admin(Request $request){
		$id=$request->param('id');
		$service=new AdminService;
		$adminUserItem=$service->selectAdminUser($id);
		$user['id']=$id;
		$user['username']=$request->param('username');
		//判断是否修改了密码
		$pass=$adminUserItem['password'];//原密码 md5格式
		$updatePass=$request->param('password');//提交的密码
		if($pass==$updatePass){
			$user['password']=$request->param('password');
		}else{
			$user['password']=md5($request->param('password'));
		}		
		$user['level']=$request->param('level');
		if($res=$service->updateAdminUser($user)){
			$this->success('用户[ ' . $res->username . ':' . $res->id . ' ]修改成功','index');
		}else{
			$this->error('用户修改失败');
		}
	}
	//ajax修改状态信息
	public function ajax_update(Request $request){		
		$id = $request->param('id');
		$state = $request->param('state');
		$user['id']=$id;		
		if($state=='true'){
			//原状态为启用,修改为禁用
			$user['status']=0;
		}else{
			//原状态为禁用,修改为启用
			$user['status']=1;
		}
		$service=new AdminService;
		if($res=$service->updateAdminUser($user)){
			if($user['status']==1){
				$jsonArr=['code'=>201,'mes'=>'修改为启用'];
			}else{
				$jsonArr=['code'=>200,'mes'=>'修改为禁用'];
			}		
			return $jsonArr;
		}
	}
	//ajax删除管理员
	public function ajax_del(Request $request){
		if($request->isAjax()){
			$service=new AdminService;
			$uid=$request->param('id');
			if($rst=$service->deleteAdminUser($uid)){
				return $rst;
			}
		}
	}
}
?>